package com.example;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

/**
 * @Description: This is a sample class.
 * @Author: Simon
 * @LastModified: 2023/12/29
 */
@Component("mqttToHttp")
public class MqttToHttpRoute extends RouteBuilder {
//    @Value("${mqtt.broker.ip}")
//    private String mqttBrokerIp;
    @Override
    public void configure() throws Exception {

        from("mqtt:myMqtt?subscribeTopicName=myTopic&host=tcp://192.168.88.158:1883")
                .autoStartup(true)
                .process(exchange -> {
                    // 处理MQTT消息，这里你可以添加逻辑来处理消息
                    String mqttMessage = exchange.getIn().getBody(String.class);
                    System.out.println("Received MQTT Message: " + mqttMessage);
                    // 可以在这里进行转换操作...
                    // 发送消息到HTTP服务器
                    exchange.getOut().setHeader("Content-Type", "text/plain");
                    exchange.getOut().setHeader("Protocol-translator","hello world!");
                    exchange.getOut().setBody(mqttMessage);
                })
                .to("http://localhost:8090");
    }
}
